/*
 * MenuEditView.java
 *
 * Created on July 15, 2008, 12:22 AM
 */
package com.indragunawan.restobiz.app;

import com.indragunawan.restobiz.app.model.MenuViewRD;
import com.indragunawan.restobiz.app.model.MenuViewTM;
import com.jidesoft.swing.AutoCompletion;
import com.jidesoft.swing.ComboBoxSearchable;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author  igoens
 */
public class MenuEditView extends javax.swing.JDialog {
    
    private static final long serialVersionUID = 5816427475861926642L;

    private EntityManager menuEntity;
    private Query menuQuery;
    private GeneralConfig cfg = new GeneralConfig();
    private List kelompokList;
    private int i;
    private NumberFormat displayMenu = new DecimalFormat("0000");
    private NumberFormat floatDisplay = new DecimalFormat("#,##0.00");

    /** Creates new form MenuEditView */
    public MenuEditView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        menuEntity = Persistence.createEntityManagerFactory("RestobizPU", cfg.getPersistanceDbProperties()).createEntityManager();

        resetInputForm();
        refreshTableMenu();
    }

    private void refreshTableMenu() {
        menuTable.setModel(new MenuViewTM(cfg.getMenuList()));
        for (i = 0; i < menuTable.getColumnCount(); i++) {
            menuTable.getColumnModel().getColumn(i).setCellRenderer(new MenuViewRD());
        }
        menuTable.revalidate();
    }

    private void fillKelompokField() {
        try {
            // Create list of kelompok
            kelompokList = cfg.getKelompokMenu();

            // Fill ComboBox with result
            kelompokField.removeAllItems();
            for (i = 0; i <= kelompokList.size() - 1; i++) {
                kelompokField.addItem(kelompokList.get(i));
            }
            
            AutoCompletion ac = new AutoCompletion(kelompokField, new ComboBoxSearchable(this.kelompokField) {
                @Override
                protected String convertElementToString(Object object) {
                        return String.valueOf(object);
                }
            });          
        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        menuTabelMenu = new javax.swing.JPopupMenu();
        ubahKelompokMenuItem = new javax.swing.JMenuItem();
        ubahNamaMenuItem = new javax.swing.JMenuItem();
        ubahHargaMenuItem = new javax.swing.JMenuItem();
        ubahSatuanMenuItem = new javax.swing.JMenuItem();
        menuSeparator = new javax.swing.JSeparator();
        nonPajakMenuItem = new javax.swing.JMenuItem();
        nonAktifMenuItem = new javax.swing.JMenuItem();
        headerPanel = new javax.swing.JPanel();
        headerLabel = new javax.swing.JLabel();
        subHeaderLabel = new javax.swing.JLabel();
        menuImageLabel = new javax.swing.JLabel();
        footerPanel = new javax.swing.JPanel();
        tutupButton = new javax.swing.JButton();
        warningLabel = new javax.swing.JLabel();
        cetakButton = new javax.swing.JButton();
        contentPanel = new javax.swing.JPanel();
        kelompokLabel = new javax.swing.JLabel();
        namaLabel = new javax.swing.JLabel();
        namaField = new javax.swing.JTextField();
        hargaLabel = new javax.swing.JLabel();
        hargaField = new javax.swing.JTextField();
        tambahButton = new javax.swing.JButton();
        menuScrollPane = new javax.swing.JScrollPane();
        menuTable = new javax.swing.JTable();
        satuanLabel = new javax.swing.JLabel();
        satuanField = new javax.swing.JTextField();
        nonPajakField = new javax.swing.JCheckBox();
        kelompokField = new javax.swing.JComboBox();
        baruCheckBox = new javax.swing.JCheckBox();
        kelompokBaruField = new javax.swing.JTextField();

        menuTabelMenu.setName("menuTabelMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getActionMap(MenuEditView.class, this);
        ubahKelompokMenuItem.setAction(actionMap.get("ubahKelompokMenu")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getResourceMap(MenuEditView.class);
        ubahKelompokMenuItem.setText(resourceMap.getString("ubahKelompokMenuItem.text")); // NOI18N
        ubahKelompokMenuItem.setName("ubahKelompokMenuItem"); // NOI18N
        menuTabelMenu.add(ubahKelompokMenuItem);

        ubahNamaMenuItem.setAction(actionMap.get("ubahNamaMenu")); // NOI18N
        ubahNamaMenuItem.setText(resourceMap.getString("ubahNamaMenuItem.text")); // NOI18N
        ubahNamaMenuItem.setName("ubahNamaMenuItem"); // NOI18N
        menuTabelMenu.add(ubahNamaMenuItem);

        ubahHargaMenuItem.setAction(actionMap.get("ubahHargaMenu")); // NOI18N
        ubahHargaMenuItem.setText(resourceMap.getString("ubahHargaMenuItem.text")); // NOI18N
        ubahHargaMenuItem.setName("ubahHargaMenuItem"); // NOI18N
        menuTabelMenu.add(ubahHargaMenuItem);

        ubahSatuanMenuItem.setAction(actionMap.get("ubahSatuanMenu")); // NOI18N
        ubahSatuanMenuItem.setText(resourceMap.getString("ubahSatuanMenuItem.text")); // NOI18N
        ubahSatuanMenuItem.setToolTipText(resourceMap.getString("ubahSatuanMenuItem.toolTipText")); // NOI18N
        ubahSatuanMenuItem.setName("ubahSatuanMenuItem"); // NOI18N
        menuTabelMenu.add(ubahSatuanMenuItem);

        menuSeparator.setName("menuSeparator"); // NOI18N
        menuTabelMenu.add(menuSeparator);

        nonPajakMenuItem.setAction(actionMap.get("ubahNonPajakItem")); // NOI18N
        nonPajakMenuItem.setText(resourceMap.getString("nonPajakMenuItem.text")); // NOI18N
        nonPajakMenuItem.setToolTipText(resourceMap.getString("nonPajakMenuItem.toolTipText")); // NOI18N
        nonPajakMenuItem.setName("nonPajakMenuItem"); // NOI18N
        menuTabelMenu.add(nonPajakMenuItem);

        nonAktifMenuItem.setAction(actionMap.get("ubahAktifMenu")); // NOI18N
        nonAktifMenuItem.setName("nonAktifMenuItem"); // NOI18N
        menuTabelMenu.add(nonAktifMenuItem);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("menuEdit"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        headerPanel.setBackground(resourceMap.getColor("headerPanel.background")); // NOI18N
        headerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        headerPanel.setName("headerPanel"); // NOI18N

        headerLabel.setFont(resourceMap.getFont("headerLabel.font")); // NOI18N
        headerLabel.setText(resourceMap.getString("headerLabel.text")); // NOI18N
        headerLabel.setName("headerLabel"); // NOI18N

        subHeaderLabel.setText(resourceMap.getString("subHeaderLabel.text")); // NOI18N
        subHeaderLabel.setName("subHeaderLabel"); // NOI18N

        menuImageLabel.setIcon(resourceMap.getIcon("menuImageLabel.icon")); // NOI18N
        menuImageLabel.setText(resourceMap.getString("menuImageLabel.text")); // NOI18N
        menuImageLabel.setName("menuImageLabel"); // NOI18N

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(headerPanelLayout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(subHeaderLabel))
                    .addComponent(headerLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 229, Short.MAX_VALUE)
                .addComponent(menuImageLabel)
                .addContainerGap())
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(headerPanelLayout.createSequentialGroup()
                        .addComponent(headerLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(subHeaderLabel))
                    .addComponent(menuImageLabel))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        footerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        footerPanel.setName("footerPanel"); // NOI18N

        tutupButton.setAction(actionMap.get("closeMenuEdit")); // NOI18N
        tutupButton.setMnemonic('T');
        tutupButton.setName("tutupButton"); // NOI18N
        tutupButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tutupButtonKeyPressed(evt);
            }
        });

        warningLabel.setText(resourceMap.getString("warningLabel.text")); // NOI18N
        warningLabel.setName("warningLabel"); // NOI18N

        cetakButton.setAction(actionMap.get("cetakMenu")); // NOI18N
        cetakButton.setMnemonic('C');
        cetakButton.setText(resourceMap.getString("cetakButton.text")); // NOI18N
        cetakButton.setName("cetakButton"); // NOI18N
        cetakButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cetakButtonKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout footerPanelLayout = new javax.swing.GroupLayout(footerPanel);
        footerPanel.setLayout(footerPanelLayout);
        footerPanelLayout.setHorizontalGroup(
            footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, footerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(warningLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cetakButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tutupButton)
                .addContainerGap())
        );

        footerPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cetakButton, tutupButton});

        footerPanelLayout.setVerticalGroup(
            footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(footerPanelLayout.createSequentialGroup()
                .addGroup(footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(footerPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(footerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cetakButton)
                            .addComponent(tutupButton)))
                    .addGroup(footerPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(warningLabel)))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        footerPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cetakButton, tutupButton});

        contentPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        contentPanel.setName("contentPanel"); // NOI18N

        kelompokLabel.setDisplayedMnemonic('K');
        kelompokLabel.setLabelFor(kelompokField);
        kelompokLabel.setText(resourceMap.getString("kelompokLabel.text")); // NOI18N
        kelompokLabel.setName("kelompokLabel"); // NOI18N

        namaLabel.setDisplayedMnemonic('M');
        namaLabel.setLabelFor(namaField);
        namaLabel.setText(resourceMap.getString("namaLabel.text")); // NOI18N
        namaLabel.setName("namaLabel"); // NOI18N

        namaField.setText(resourceMap.getString("namaField.text")); // NOI18N
        namaField.setName("namaField"); // NOI18N
        namaField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                namaFieldKeyPressed(evt);
            }
        });

        hargaLabel.setDisplayedMnemonic('H');
        hargaLabel.setLabelFor(hargaField);
        hargaLabel.setText(resourceMap.getString("hargaLabel.text")); // NOI18N
        hargaLabel.setName("hargaLabel"); // NOI18N

        hargaField.setText(resourceMap.getString("hargaField.text")); // NOI18N
        hargaField.setName("hargaField"); // NOI18N
        hargaField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                hargaFieldKeyPressed(evt);
            }
        });

        tambahButton.setAction(actionMap.get("tambahMenu")); // NOI18N
        tambahButton.setMnemonic('a');
        tambahButton.setName("tambahButton"); // NOI18N
        tambahButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tambahButtonKeyPressed(evt);
            }
        });

        menuScrollPane.setName("menuScrollPane"); // NOI18N

        menuTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Menu", "Kelompok", "Nama", "Harga", "Satuan", "Pajak", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.String.class, java.lang.Object.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        menuTable.setName("menuTable"); // NOI18N
        menuTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                menuTableMouseClicked(evt);
            }
        });
        menuScrollPane.setViewportView(menuTable);
        menuTable.getColumnModel().getColumn(0).setMaxWidth(50);
        menuTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("menuTable.columnModel.title0")); // NOI18N
        menuTable.getColumnModel().getColumn(1).setMaxWidth(80);
        menuTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("menuTable.columnModel.title1")); // NOI18N
        menuTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("menuTable.columnModel.title2")); // NOI18N
        menuTable.getColumnModel().getColumn(3).setMaxWidth(100);
        menuTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("menuTable.columnModel.title3")); // NOI18N
        menuTable.getColumnModel().getColumn(4).setMaxWidth(50);
        menuTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("menuTable.columnModel.title5")); // NOI18N
        menuTable.getColumnModel().getColumn(5).setMaxWidth(50);
        menuTable.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("menuTable.columnModel.title6")); // NOI18N
        menuTable.getColumnModel().getColumn(6).setMaxWidth(50);
        menuTable.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("menuTable.columnModel.title4")); // NOI18N

        satuanLabel.setDisplayedMnemonic('S');
        satuanLabel.setLabelFor(satuanField);
        satuanLabel.setText(resourceMap.getString("satuanLabel.text")); // NOI18N
        satuanLabel.setName("satuanLabel"); // NOI18N

        satuanField.setText(resourceMap.getString("satuanField.text")); // NOI18N
        satuanField.setName("satuanField"); // NOI18N

        nonPajakField.setMnemonic('N');
        nonPajakField.setText(resourceMap.getString("nonPajakField.text")); // NOI18N
        nonPajakField.setName("nonPajakField"); // NOI18N

        kelompokField.setEditable(true);
        kelompokField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MAKANAN", "MINUMAN" }));
        kelompokField.setDoubleBuffered(true);
        kelompokField.setName("kelompokField"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, baruCheckBox, org.jdesktop.beansbinding.ELProperty.create("${!selected}"), kelompokField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        baruCheckBox.setText(resourceMap.getString("baruCheckBox.text")); // NOI18N
        baruCheckBox.setName("baruCheckBox"); // NOI18N

        kelompokBaruField.setText(resourceMap.getString("kelompokBaruField.text")); // NOI18N
        kelompokBaruField.setName("kelompokBaruField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, baruCheckBox, org.jdesktop.beansbinding.ELProperty.create("${selected}"), kelompokBaruField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(menuScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 651, Short.MAX_VALUE)
                    .addGroup(contentPanelLayout.createSequentialGroup()
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(namaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(kelompokLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kelompokField, 0, 210, Short.MAX_VALUE)
                            .addComponent(namaField, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
                        .addGap(12, 12, 12)
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(hargaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(baruCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(contentPanelLayout.createSequentialGroup()
                                .addComponent(hargaField, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(satuanLabel))
                            .addComponent(kelompokBaruField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(contentPanelLayout.createSequentialGroup()
                                .addComponent(satuanField, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tambahButton))
                            .addComponent(nonPajakField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kelompokLabel)
                    .addComponent(kelompokField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nonPajakField)
                    .addComponent(kelompokBaruField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(baruCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tambahButton)
                    .addComponent(satuanLabel)
                    .addComponent(satuanField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hargaLabel)
                    .addComponent(namaLabel)
                    .addComponent(namaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hargaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(menuScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(footerPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(footerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    resetInputForm();
}//GEN-LAST:event_formWindowOpened

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    try {
        menuEntity.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_formWindowClosing

private void namaFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_namaFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        evt.getComponent().transferFocus();
    }
}//GEN-LAST:event_namaFieldKeyPressed

private void hargaFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_hargaFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        evt.getComponent().transferFocus();
    }
}//GEN-LAST:event_hargaFieldKeyPressed

private void tambahButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tambahButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        tambahMenu();
    }
}//GEN-LAST:event_tambahButtonKeyPressed

private void menuTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menuTableMouseClicked
    if (menuTable.getSelectedRowCount() == 1) {
        if ((evt.getClickCount() == 1) && (evt.getButton() == MouseEvent.BUTTON3)) {
            menuTabelMenu.show(menuTable, evt.getX(), evt.getY());
        }
    }
}//GEN-LAST:event_menuTableMouseClicked

private void cetakButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cetakButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        cetakMenu();
    }
}//GEN-LAST:event_cetakButtonKeyPressed

private void tutupButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tutupButtonKeyPressed
    closeMenuEdit();
}//GEN-LAST:event_tutupButtonKeyPressed

    @Action
    public void closeMenuEdit() {
        this.dispose();
    }

    public void clearInputForm() {
        fillKelompokField();
        namaField.setText("");
        hargaField.setText("");
        satuanField.setText("");
        kelompokField.setSelectedIndex(-1);
        kelompokBaruField.setText("");
        baruCheckBox.setSelected(false);
        nonPajakField.setSelected(false);        
    }

    @Action
    public void resetInputForm() {
        clearInputForm();
        kelompokField.requestFocus();
    }

    @Action
    public void tambahMenu() {
        
        String kelompok = "";

        if (baruCheckBox.isSelected()) {
            kelompok = kelompokBaruField.getText();
        } else {
            kelompok = String.valueOf(kelompokField.getSelectedItem());
        }

        if (!kelompok.isEmpty() && !namaField.getText().isEmpty() && !(Double.valueOf(hargaField.getText()) <= 0)) {
            try {
                menuEntity.getTransaction().begin();
                menuQuery = menuEntity.createNativeQuery("INSERT INTO menu VALUES (null, #kelompok, #nama, #harga, #aktif, #pajak, #satuan)").setParameter("kelompok", kelompok).setParameter("nama", namaField.getText()).setParameter("harga", Double.valueOf(hargaField.getText())).setParameter("aktif", true).setParameter("pajak", !nonPajakField.isSelected()).setParameter("satuan", String.valueOf(satuanField.getText()));
                menuQuery.executeUpdate();
                menuEntity.getTransaction().commit();
                resetInputForm();
                refreshTableMenu();

            } catch (Exception ex) {
                menuEntity.getTransaction().rollback();
                ex.printStackTrace();
                Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            JOptionPane.showMessageDialog(null, "Harap isi seluruh field yang tersedia");
            kelompokField.requestFocus();
        }
    }

    @Action
    public void ubahKelompokMenu() {
        try {
            Integer Menu;
            String kelompokLama = null;
            String kelompokBaru = null;

            kelompokLama = String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 1));
            kelompokBaru = JOptionPane.showInputDialog(menuTable, "Ganti Kelompok Menu", kelompokLama);

            if (!kelompokLama.equals(kelompokBaru) && !(kelompokBaru == null)) {
                Menu = Integer.valueOf(String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 0)));

                menuEntity.getTransaction().begin();
                menuQuery = menuEntity.createNativeQuery("UPDATE menu m SET m.kelompok = #kelompok WHERE m.menu = #menu").setParameter("menu", displayMenu.format(Menu)).setParameter("kelompok", kelompokBaru);
                menuQuery.executeUpdate();
                menuEntity.getTransaction().commit();
            }
        } catch (Exception ex) {
            menuEntity.getTransaction().rollback();
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        refreshTableMenu();
    }

    @Action
    public void ubahNamaMenu() {
        try {
            Integer Menu;
            String namaLama = null;
            String namaBaru = null;

            namaLama = String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 2));
            namaBaru = JOptionPane.showInputDialog(menuTable, "Ganti Nama Menu", namaLama);

            if (!namaLama.equals(namaBaru) && !(namaBaru == null)) {
                Menu = Integer.valueOf(String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 0)));

                menuEntity.getTransaction().begin();
                menuQuery = menuEntity.createNativeQuery("UPDATE menu m SET m.nama = #nama WHERE m.menu = #menu").setParameter("menu", displayMenu.format(Menu)).setParameter("nama", namaBaru);
                menuQuery.executeUpdate();
                menuEntity.getTransaction().commit();
            }
        } catch (Exception ex) {
            menuEntity.getTransaction().rollback();
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        refreshTableMenu();
    }

    @Action
    public void ubahHargaMenu() {
        try {
            Integer Menu;
            String inputDialog;
            Double hargaLama = null;
            Double hargaBaru = null;

            hargaLama = Double.valueOf(String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 3)));
            inputDialog = JOptionPane.showInputDialog(menuTable, "Ganti Harga Menu", floatDisplay.format(hargaLama));

            if (!(inputDialog == null) && (!inputDialog.isEmpty())) {
                hargaBaru = Double.valueOf(inputDialog);
            }

            if (!hargaLama.equals(hargaBaru) && !(hargaBaru == null)) {
                Menu = Integer.valueOf(String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 0)));

                menuEntity.getTransaction().begin();
                menuQuery = menuEntity.createNativeQuery("UPDATE menu m SET m.harga = #harga WHERE m.menu = #menu").setParameter("menu", displayMenu.format(Menu)).setParameter("harga", hargaBaru);
                menuQuery.executeUpdate();
                menuEntity.getTransaction().commit();
            }
        } catch (Exception ex) {
            menuEntity.getTransaction().rollback();
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        refreshTableMenu();

    }

    @Action
    public void ubahAktifMenu() {
        try {
            Integer Menu;

            Menu = Integer.valueOf(String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 0)));

            menuEntity.getTransaction().begin();
            menuQuery = menuEntity.createNativeQuery("UPDATE menu m SET m.aktif = not (m.aktif) WHERE m.menu = #menu").setParameter("menu", displayMenu.format(Menu));
            menuQuery.executeUpdate();
            menuEntity.getTransaction().commit();

        } catch (Exception ex) {
            menuEntity.getTransaction().rollback();
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        refreshTableMenu();
    }

    @Action
    public void cetakMenu() {
        cfg.printMenuList();
    }

    @Action
    public void ubahNonPajakItem() {
        try {
            Integer Menu;

            Menu = Integer.valueOf(String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 0)));

            menuEntity.getTransaction().begin();
            menuQuery = menuEntity.createNativeQuery("UPDATE menu m SET m.pajak = not (m.pajak) WHERE m.menu = #menu").setParameter("menu", displayMenu.format(Menu));
            menuQuery.executeUpdate();
            menuEntity.getTransaction().commit();

        } catch (Exception ex) {
            menuEntity.getTransaction().rollback();
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        refreshTableMenu();
    }

    @Action
    public void ubahSatuanMenu() {
        try {
            Integer Menu;
            String satuanLama = null;
            String satuanBaru = null;

            satuanLama = String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 4));
            satuanBaru = JOptionPane.showInputDialog(menuTable, "Ganti Satuan Menu", satuanLama);

            if (!satuanLama.equals(satuanBaru) && !(satuanBaru == null)) {
                Menu = Integer.valueOf(String.valueOf(menuTable.getValueAt(menuTable.getSelectedRow(), 0)));

                menuEntity.getTransaction().begin();
                menuQuery = menuEntity.createNativeQuery("UPDATE menu m SET m.satuan = #satuan WHERE m.menu = #menu").setParameter("menu", displayMenu.format(Menu)).setParameter("satuan", satuanBaru);
                menuQuery.executeUpdate();
                menuEntity.getTransaction().commit();
            }
        } catch (Exception ex) {
            menuEntity.getTransaction().rollback();
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        refreshTableMenu();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox baruCheckBox;
    private javax.swing.JButton cetakButton;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel footerPanel;
    private javax.swing.JTextField hargaField;
    private javax.swing.JLabel hargaLabel;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JTextField kelompokBaruField;
    private javax.swing.JComboBox kelompokField;
    private javax.swing.JLabel kelompokLabel;
    private javax.swing.JLabel menuImageLabel;
    private javax.swing.JScrollPane menuScrollPane;
    private javax.swing.JSeparator menuSeparator;
    private javax.swing.JPopupMenu menuTabelMenu;
    private javax.swing.JTable menuTable;
    private javax.swing.JTextField namaField;
    private javax.swing.JLabel namaLabel;
    private javax.swing.JMenuItem nonAktifMenuItem;
    private javax.swing.JCheckBox nonPajakField;
    private javax.swing.JMenuItem nonPajakMenuItem;
    private javax.swing.JTextField satuanField;
    private javax.swing.JLabel satuanLabel;
    private javax.swing.JLabel subHeaderLabel;
    private javax.swing.JButton tambahButton;
    private javax.swing.JButton tutupButton;
    private javax.swing.JMenuItem ubahHargaMenuItem;
    private javax.swing.JMenuItem ubahKelompokMenuItem;
    private javax.swing.JMenuItem ubahNamaMenuItem;
    private javax.swing.JMenuItem ubahSatuanMenuItem;
    private javax.swing.JLabel warningLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
